//
//  File.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/5/16.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation
/*
 单调栈的解法
 */
class Solution739 {
    func dailyTemperatures(_ temperatures: [Int]) -> [Int] {
        var stack: [Int] = []
        var array: [Int] = Array(repeating: 0, count: temperatures.count)
        for i in 0..<temperatures.count {
            // 维持单调栈
            while !stack.isEmpty && temperatures[i] > temperatures[stack.last!] {
                let index = stack.removeLast()
                array[index] = i - index
            }
            stack.append(i)
        }
        return array
    }
    static func test(){
        let array = [73, 74, 75, 71, 69, 72, 76, 73]
        let solution = Solution739()
        let result = solution.dailyTemperatures(array)
        print(result)
    }
}
